package jjn.daily;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-09-14 23:38
 */
public class LeetCode1222 {
    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        boolean[][] queenExisted = new boolean[8][8];
        for (int[] position : queens) {
            queenExisted[position[0]][position[1]] = true;
        }
        List<List<Integer>> result = new ArrayList<>();
        int y = king[0], x = king[1];
        for (int i = x; i < 8; i++) {
            if (queenExisted[y][i]) {
                result.add(List.of(y, i));
                break;
            }
        }
        for (int i = x; i >= 0; i--) {
            if (queenExisted[y][i]) {
                result.add(List.of(y, i));
                break;
            }
        }
        for (int i = y; i < 8; i++) {
            if (queenExisted[i][x]) {
                result.add(List.of(i, x));
                break;
            }
        }
        for (int i = y; i >= 0; i--) {
            if (queenExisted[i][x]) {
                result.add(List.of(i, x));
                break;
            }
        }
        for (int i = y, j = x; i < 8 && j < 8; i++, j++) {
            if (queenExisted[i][j]) {
                result.add(List.of(i, j));
                break;
            }
        }
        for (int i = y, j = x; i >= 0 && j >= 0; i--, j--) {
            if (queenExisted[i][j]) {
                result.add(List.of(i, j));
                break;
            }
        }
        for (int i = y, j = x; i < 8 && j >= 0; i++, j--) {
            if (queenExisted[i][j]) {
                result.add(List.of(i, j));
                break;
            }
        }
        for (int i = y, j = x; i >= 0 && j < 8; i--, j++) {
            if (queenExisted[i][j]) {
                result.add(List.of(i, j));
                break;
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
        int[][] queens = new int[][]{{5, 6}, {7, 7}, {2, 1}, {0, 7}, {1, 6}, {5, 1}, {3, 7}, {0, 3}, {4, 0}, {1, 2}, {6, 3}, {5, 0}, {0, 4}, {2
                , 2}, {1, 1}, {6, 4}, {5, 4}, {0, 0}, {2, 6}, {4, 5}, {5, 2}, {1, 4}, {7, 5}, {2, 3}, {0, 5}, {4, 2}, {1, 0}, {2, 7}, {0, 1}, {4, 6}, {6, 1}, {0, 6}, {4, 3}, {1, 7}};
        List<List<Integer>> lists = new LeetCode1222().queensAttacktheKing(queens, new int[]{3, 4});
        System.out.println(lists.toString());
    }
}
